﻿using System;
using System.Collections;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace SiteFox.WebParts.Blog
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]

    public class CommentsWP : WebPart
    {
        #region Controls
        Xml _Xml;
        //=================================================================
        Panel _pan_CommentAdd;
        Panel _pan_CommentAdd_TitleRow;
        Panel _pan_CommentAdd_CommentRow;
        Panel _pan_CommentAdd_ControlRow;
        //=================================================================
        Label _lab_CommentAdd_TitleRow_Title;
        Label _lab_CommentAdd_CommentRow_Title;
        //=================================================================
        TextBox _tb_CommentAdd_TitleRow_Value;
        TextBox _tb_CommentAdd_CommentRow_Value;
        //=================================================================
        Button _btn_CommentAdd_Submit;
        //=================================================================
        RequiredFieldValidator _rfv_CommentAdd_Title;
        RequiredFieldValidator _rfv_CommentAdd_Comment;
        //=================================================================
        #endregion

        #region PublicProperies
        private string _XsltUrl = "~/System/Design/xslt/WebParts/Blog/Comments.xslt";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("")]
        [WebDisplayName("XSL-T url")]
        public string XsltUrl
        {
            get { return _XsltUrl; }
            set { _XsltUrl = value; }
        }

        private bool _AllowAnonymouseComments = false;
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("")]
        [WebDisplayName("Allow anonymouse comments")]
        public bool AllowAnonymouseComments
        {
            get { return _AllowAnonymouseComments; }
            set { _AllowAnonymouseComments = value; }
        }

        private string _AddCommentCssClass = "Blog_CommentsWP_AddComment";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("")]
        [WebDisplayName("Add comment CssClass")]
        public string AddCommentCssClass
        {
            get { return _AddCommentCssClass; }
            set { _AddCommentCssClass = value; }
        }
        #endregion

        protected override void CreateChildControls()
        {
            Controls.Clear();
            if (HttpContext.Current.Request.QueryString["Id"] != null)
            {
                int _QueryId = -1;
                try { _QueryId = Convert.ToInt32(HttpContext.Current.Request.QueryString["Id"].ToString()); }
                catch { }
                if (_QueryId > -1)
                {
                    _Xml = new Xml();
                    this.LoadComments(_QueryId);
                    Controls.Add(_Xml);
                    if (HttpContext.Current.User.Identity.IsAuthenticated || _AllowAnonymouseComments)
                    {
                        _pan_CommentAdd = new Panel();
                        _pan_CommentAdd.CssClass = _AddCommentCssClass;
                        Controls.Add(_pan_CommentAdd);
                        //===== TitleRow ============================================================
                        _pan_CommentAdd_TitleRow = new Panel();
                        _pan_CommentAdd_TitleRow.CssClass = _AddCommentCssClass + "_Row";
                        _pan_CommentAdd.Controls.Add(_pan_CommentAdd_TitleRow);
                        _lab_CommentAdd_TitleRow_Title = new Label();
                        _lab_CommentAdd_TitleRow_Title.Text = "Title";
                        _lab_CommentAdd_TitleRow_Title.CssClass = _AddCommentCssClass + "_RowTitle";
                        _pan_CommentAdd_TitleRow.Controls.Add(_lab_CommentAdd_TitleRow_Title);
                        _tb_CommentAdd_TitleRow_Value = new TextBox();
                        _tb_CommentAdd_TitleRow_Value.CssClass = _AddCommentCssClass + "_RowValue";
                        _tb_CommentAdd_TitleRow_Value.MaxLength = 50;
                        _tb_CommentAdd_TitleRow_Value.ID = this.ID + "_tb_CommentAdd_TitleRow_Value";
                        _pan_CommentAdd_TitleRow.Controls.Add(_tb_CommentAdd_TitleRow_Value);
                        _rfv_CommentAdd_Title = new RequiredFieldValidator();
                        _rfv_CommentAdd_Title.ControlToValidate = _tb_CommentAdd_TitleRow_Value.ID;
                        _rfv_CommentAdd_Title.CssClass = _AddCommentCssClass + "_RowValidator";
                        _rfv_CommentAdd_Title.Text = "*";
                        _rfv_CommentAdd_Title.ErrorMessage = "*";
                        _rfv_CommentAdd_Title.ValidationGroup = this.ID + "_ValidationGroup";
                        _pan_CommentAdd_TitleRow.Controls.Add(_rfv_CommentAdd_Title);
                        //===== CommentRow ==========================================================
                        _pan_CommentAdd_CommentRow = new Panel();
                        _pan_CommentAdd_CommentRow.CssClass = _AddCommentCssClass + "_Row";
                        _pan_CommentAdd.Controls.Add(_pan_CommentAdd_CommentRow);
                        _lab_CommentAdd_CommentRow_Title = new Label();
                        _lab_CommentAdd_CommentRow_Title.Text = "Comment";
                        _lab_CommentAdd_CommentRow_Title.CssClass = _AddCommentCssClass + "_RowTitle";
                        _pan_CommentAdd_CommentRow.Controls.Add(_lab_CommentAdd_CommentRow_Title);
                        _tb_CommentAdd_CommentRow_Value = new TextBox();
                        _tb_CommentAdd_CommentRow_Value.CssClass = _AddCommentCssClass + "_RowValue";
                        _tb_CommentAdd_CommentRow_Value.TextMode = TextBoxMode.MultiLine;
                        _tb_CommentAdd_CommentRow_Value.ID = this.ID + "_tb_CommentAdd_CommentRow_Value";
                        _pan_CommentAdd_CommentRow.Controls.Add(_tb_CommentAdd_CommentRow_Value);
                        _rfv_CommentAdd_Comment = new RequiredFieldValidator();
                        _rfv_CommentAdd_Comment.ControlToValidate = _tb_CommentAdd_CommentRow_Value.ID;
                        _rfv_CommentAdd_Comment.CssClass = _AddCommentCssClass + "_RowValidator";
                        _rfv_CommentAdd_Comment.Text = "*";
                        _rfv_CommentAdd_Comment.ErrorMessage = "*";
                        _rfv_CommentAdd_Comment.ValidationGroup = this.ID + "_ValidationGroup";
                        _pan_CommentAdd_CommentRow.Controls.Add(_rfv_CommentAdd_Comment);
                        //===== ControlRow ==========================================================
                        _pan_CommentAdd_ControlRow = new Panel();
                        _pan_CommentAdd_ControlRow.CssClass = _AddCommentCssClass + "_Row";
                        _pan_CommentAdd.Controls.Add(_pan_CommentAdd_ControlRow);
                        _btn_CommentAdd_Submit = new Button();
                        _btn_CommentAdd_Submit.Text = "Submit";
                        _btn_CommentAdd_Submit.CssClass = _AddCommentCssClass + "_Button";
                        _btn_CommentAdd_Submit.Click += new EventHandler(_btn_CommentAdd_Submit_Click);
                        _btn_CommentAdd_Submit.ValidationGroup = this.ID + "_ValidationGroup";
                        _pan_CommentAdd_ControlRow.Controls.Add(_btn_CommentAdd_Submit);
                    }
                }
            }
        }
        void _btn_CommentAdd_Submit_Click(object sender, EventArgs e)
        {
            int _QueryId = Convert.ToInt32(HttpContext.Current.Request.QueryString["Id"].ToString());
            SiteFox.Business.Model.BlogComment _Comment = new Business.Model.BlogComment();
            _Comment.Title = _tb_CommentAdd_TitleRow_Value.Text;
            _Comment.Text = _tb_CommentAdd_CommentRow_Value.Text;
            _Comment.ID_BlogPost = _QueryId;
            _Comment.Created = DateTime.Now;
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                _Comment.CreatedBy = HttpContext.Current.User.Identity.Name;
            }
            else 
            {
                _Comment.CreatedBy = "[Guest]";
            }
            SiteFox.Business.Manager.BlogComment.Create(_Comment);
            _tb_CommentAdd_CommentRow_Value.Text = "";
            _tb_CommentAdd_TitleRow_Value.Text = "";
            this.LoadComments(_QueryId);
        }
        void LoadComments(int _QueryId) 
        { 
            _Xml.TransformSource = _XsltUrl;
            XmlDocument _XmlDocument = new XmlDocument();
            XmlDeclaration _XmlDeclaration = _XmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement _XmlComments = _XmlDocument.CreateElement("Comments");
            _XmlDocument.InsertBefore(_XmlDeclaration, _XmlDocument.DocumentElement);
            _XmlDocument.AppendChild(_XmlComments);
            var _Comments = SiteFox.Business.Manager.BlogComment.GetByBlogPost(_QueryId);
            for (int i = 0; i < _Comments.Count; i++)
            {
                var __Comment = _Comments[i];
                XmlElement _XmlComment = _XmlDocument.CreateElement("Comment");
                _XmlComment.SetAttribute("Id", __Comment.ID.ToString());
                _XmlComment.SetAttribute("Date", __Comment.Created.Value.ToShortDateString());
                _XmlComment.SetAttribute("Time", __Comment.Created.Value.ToShortTimeString());
                _XmlComment.SetAttribute("CreatedBy", __Comment.CreatedBy);
                _XmlComments.AppendChild(_XmlComment);
                XmlElement _XmlCommentTitle = _XmlDocument.CreateElement("Title");
                _XmlCommentTitle.InnerText = __Comment.Title;
                _XmlComment.AppendChild(_XmlCommentTitle);
                XmlElement _XmlCommentText = _XmlDocument.CreateElement("Text");
                _XmlCommentText.InnerText = __Comment.Text;
                _XmlComment.AppendChild(_XmlCommentText);
            }
            _Xml.DocumentContent = _XmlDocument.InnerXml;
        }
    }
}

